package visiopuce.DAO;

import java.math.BigDecimal;
import java.util.List;

import org.hibernate.Query;

import visiopuce.HibernateUtil;
import visiopuce.objets.Resultat;

public class ResultatDAOImpl extends GenericDAOImpl<Resultat, BigDecimal> implements ResultatDAO {
	/** Constructeur priv� */
	private ResultatDAOImpl() {
	}

	/** Instance unique non pr�initialis�e */
	private static ResultatDAOImpl INSTANCE = null;

	/** Point d'acc�s pour l'instance unique du singleton */
	public static ResultatDAO getInstance() {
		if (INSTANCE == null) {
			INSTANCE = new ResultatDAOImpl();
		}
		return INSTANCE;
	}

	@Override
	public List<Resultat> findAllByidAnalyse(Integer idAnalyse) {
		List<Resultat> resultats = null;
		String sql = "SELECT r FROM Resultat r WHERE r.analyse.idAnalyse = :idAnalyse";
		Query query = HibernateUtil.getSession().createQuery(sql).setParameter("idAnalyse", idAnalyse);
		resultats = findMany(query);
		return resultats;
	}

	@Override
	public String getTypeAnalyseLibelleByidResultat(int idResultat) {
		String typeAna = null;
		String sql = "SELECT r.analyse.typeAnalyse.libelle FROM Resultat r WHERE r.idResultat = :idResultat";
		Query query = HibernateUtil.getSession().createQuery(sql).setParameter("idResultat", idResultat);
		typeAna = (String) query.uniqueResult();
		return typeAna;
	}

	@Override
	public List<Resultat> getResultatByIdPuce(Integer idPuce) {

		List<Resultat> resultats = null;
		String sql = "SELECT rp.resultat FROM RenduPuce rp WHERE rp.puce.idPuce = :idPuce";
		Query query = HibernateUtil.getSession().createQuery(sql).setParameter("idPuce", idPuce);
		resultats = findMany(query);

		return resultats;
	}

	@Override
	public List<Resultat> getResulatMicrosatByIdAnalysePuce(Integer idAnalyse) {
		List<Resultat> resultats = null;
		String sql = "SELECT r2 FROM Resultat r, Resultat r2 WHERE r.analyse.prelevement.secteur.idSecteur = 3 AND  r.analyse.idAnalyse = :idAnalyse and r2.analyse.prelevement.idPrelevement = r.analyse.prelevement.idPrelevement and "
				+ "r.analyse.idAnalyse != r2.analyse.idAnalyse and r2.analyse.typeAnalyse.idTypeAnalyse = 6 ";
		Query query = HibernateUtil.getSession().createQuery(sql).setParameter("idAnalyse", idAnalyse);
		resultats = findMany(query);
		return resultats;
	}

	@Override
	public List<String> getFormuleResultatByIdPuce(Integer idPuce) {
		List<String> resultats = null;
		String sql = "SELECT rp.resultat.formule FROM RenduPuce rp WHERE rp.puce.idPuce = :idPuce AND rp.resultat.dateRendu is not null";
		Query query = HibernateUtil.getSession().createQuery(sql).setParameter("idPuce", idPuce);
		resultats = (List<String>) query.list();

		return resultats;
	}

}